What is it-map?
The it-map npm package is a utility for transforming async iterables. It allows you to apply a mapping function to each item in an async iterable, similar to how Array.prototype.map works for arrays.
What are it-map's main functionalities?
Basic Mapping
This feature allows you to apply a simple mapping function to each item in an async iterable. In this example, each number yielded by the async generator is doubled.
const itMap = require('it-map');
async function* asyncGenerator() {
yield 1;
yield 2;
yield 3;
}
const mapped = itMap(asyncGenerator(), x => x * 2);
(async () => {
for await (const value of mapped) {
console.log(value); // 2, 4, 6
}
})();
Async Mapping Function
This feature allows you to use an async function as the mapping function. In this example, each number is doubled after a 100ms delay.
const itMap = require('it-map');
async function* asyncGenerator() {
yield 1;
yield 2;
yield 3;
}
const mapped = itMap(asyncGenerator(), async x => {
await new Promise(resolve => setTimeout(resolve, 100));
return x * 2;
});
(async () => {
for await (const value of mapped) {
console.log(value); // 2, 4, 6
}
})();
Other packages similar to it-map
it-pipe
The it-pipe package is used for creating pipelines of async iterables. It allows you to compose multiple transformations in a readable manner. Compared to it-map, it-pipe provides a more comprehensive solution for chaining multiple async iterable transformations.
it-filter
The it-filter package is used for filtering items in async iterables. While it-map focuses on transforming each item, it-filter focuses on including or excluding items based on a predicate function. Both can be used together for more complex async iterable manipulations.
it-take
The it-take package is used for taking a specified number of items from an async iterable. This is useful for limiting the number of items processed, which can be combined with it-map for more controlled transformations.
it-map
Maps the values yielded by an async iterator
About
Convert one value from an (async)iterator into another.
Example
import map from 'it-map'
const values = [0, 1, 2, 3, 4]
const result = map(values, (val, index) => val++)
console.info(result)
Async sources and transforms must be awaited:
import map from 'it-map'
const values = async function * () {
yield * [0, 1, 2, 3, 4]
}
const result = await map(values(), async (val, index) => val++)
console.info(result)
Install
$ npm i it-map
Browser <script>
tag
Loading this module through a script tag will make it's exports available as ItMap
in the global namespace.
<script src="https://unpkg.com/it-map/dist/index.min.js"></script>
API Docs
License
Licensed under either of
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.